package top.luhancc.mztbizlog.controller;

import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.luhancc.mztbizlog.model.UserInfo;

import java.time.LocalDateTime;

/**
 * @author luHan
 * @create 2025/8/15 10:30
 * @since 1.0.0
 */
@RestController
@RequestMapping("/mztBizLog")
public class MztBizLogController {

    @PostMapping("/addLog")
    @LogRecord(success = "新增日志测试，用户名：「{{#userInfo.userName}}」，昵称：「{{#userInfo.nickName}}」，头像：「{{#userInfo.nickName}}」，操作时间：「{{#createTime}}」",
            fail = "方法失败，失败原因：「{{#_errorMsg}}」",
            type = "add",
            bizNo = "",
            extra = "{{#userInfo.toString()}}")
    public void addLog(@RequestBody UserInfo userInfo) {
        LogRecordContext.putVariable("createTime", LocalDateTime.now());
        if (userInfo.getUserName().equals("testFail")) {
            throw new RuntimeException("保存用户是发生了异常");
        }
    }
}
